import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestBinarySearch {

    @Test
    @DisplayName("binarySearchBasic 找到")
    public void test1(){
        int[] a = {7, 13, 21, 30, 38, 44, 52, 57};
        assertEquals(0 , BinarySearchBasic.binarySearchBasic(a, 7));
        assertEquals(1 , BinarySearchBasic.binarySearchBasic(a, 13));
        assertEquals(2 , BinarySearchBasic.binarySearchBasic(a, 21));
        assertEquals(3 , BinarySearchBasic.binarySearchBasic(a, 30));
        assertEquals(4 , BinarySearchBasic.binarySearchBasic(a, 38));
        assertEquals(5 , BinarySearchBasic.binarySearchBasic(a, 44));
        assertEquals(6 , BinarySearchBasic.binarySearchBasic(a, 52));
        assertEquals(7 , BinarySearchBasic.binarySearchBasic(a, 57));
    }


    @Test
    @DisplayName("binarySearchBasic 找不到")
    public void test2(){
        int[] a = {7, 13, 21, 30, 38, 44, 52, 57};
        assertEquals(-1, BinarySearchBasic.binarySearchBasic(a, 2));
        assertEquals(-1, BinarySearchBasic.binarySearchBasic(a, 31));
        assertEquals(-1, BinarySearchBasic.binarySearchBasic(a, 90));
    }

    /**
     * 无符号右移运算符
     */
    @Test
    public void test3(){
        int j = Integer.MAX_VALUE - 1; // 2147483646
        int i = j >>> 1; // 1073741823
        System.out.println((i + j) / 2);  // -536870913
        System.out.println(i + j);  // -1073741827
        System.out.println((i + j) >>> 1); // -1073741827 >>> 1  = 1610612734

        System.out.println(10 >>> 2);  // => 10 / 2 / 2 = 2
    }


    @Test
    @DisplayName("判断binarySearchAlter 找到")
    public void test4(){
        int[] a = {7, 13, 21, 30, 38, 44, 52, 57};
        assertEquals(0 , BinarySearchAlter.binarySearchAlter(a, 7));
        assertEquals(1 , BinarySearchAlter.binarySearchAlter(a, 13));
        assertEquals(2 , BinarySearchAlter.binarySearchAlter(a, 21));
        assertEquals(3 , BinarySearchAlter.binarySearchAlter(a, 30));
        assertEquals(4 , BinarySearchAlter.binarySearchAlter(a, 38));
        assertEquals(5 , BinarySearchAlter.binarySearchAlter(a, 44));
        assertEquals(6 , BinarySearchAlter.binarySearchAlter(a, 52));
        assertEquals(7 , BinarySearchAlter.binarySearchAlter(a, 57));
    }


    @Test
    @DisplayName("判断binarySearchAlter 找不到")
    public void test5(){
        int[] a = {7, 13, 21, 30, 38, 44, 52, 57};
        assertEquals(-1, BinarySearchAlter.binarySearchAlter(a, 2));
        assertEquals(-1, BinarySearchAlter.binarySearchAlter(a, 31));
        assertEquals(-1, BinarySearchAlter.binarySearchAlter(a, 90));
    }

    @Test
    @DisplayName("判断binarySearchBalance 找到")
    public void test6(){
        int[] a = {7, 13, 21, 30, 38, 44, 52, 57};
        assertEquals(0 , BinarySearchBalance.binarySearchBalance(a, 7));
        assertEquals(1 , BinarySearchBalance.binarySearchBalance(a, 13));
        assertEquals(2 , BinarySearchBalance.binarySearchBalance(a, 21));
        assertEquals(3 , BinarySearchBalance.binarySearchBalance(a, 30));
        assertEquals(4 , BinarySearchBalance.binarySearchBalance(a, 38));
        assertEquals(5 , BinarySearchBalance.binarySearchBalance(a, 44));
        assertEquals(6 , BinarySearchBalance.binarySearchBalance(a, 52));
        assertEquals(7 , BinarySearchBalance.binarySearchBalance(a, 57));
    }


    @Test
    @DisplayName("判断binarySearchBalance 找不到")
    public void test7(){
        int[] a = {7, 13, 21, 30, 38, 44, 52, 57};
        assertEquals(-1, BinarySearchBalance.binarySearchBalance(a, 2));
        assertEquals(-1, BinarySearchBalance.binarySearchBalance(a, 31));
        assertEquals(-1, BinarySearchBalance.binarySearchBalance(a, 90));
    }

    @Test
    @DisplayName("判断binarySearchLeftMost ")
    public void test8(){
        int[] a = {7, 13, 21, 30, 38, 44, 52, 57};
        assertEquals(0 , BinarySearchLeftMost.binarySearchLeftMost(a, 7));
        assertEquals(1 , BinarySearchLeftMost.binarySearchLeftMost(a, 13));
        assertEquals(2 , BinarySearchLeftMost.binarySearchLeftMost(a, 21));
        assertEquals(3 , BinarySearchLeftMost.binarySearchLeftMost(a, 30));
        assertEquals(4 , BinarySearchLeftMost.binarySearchLeftMost(a, 38));
        assertEquals(5 , BinarySearchLeftMost.binarySearchLeftMost(a, 44));
        assertEquals(6 , BinarySearchLeftMost.binarySearchLeftMost(a, 52));
        assertEquals(7 , BinarySearchLeftMost.binarySearchLeftMost(a, 57));
    }

    @Test
    @DisplayName("判断binarySearchLeftMost 找不到")
    public void test9(){
        int[] a = {7, 13, 21, 30, 38, 44, 52, 57};
        assertEquals(-1, BinarySearchLeftMost.binarySearchLeftMost(a, 2));
        assertEquals(-1, BinarySearchLeftMost.binarySearchLeftMost(a, 31));
        assertEquals(-1, BinarySearchLeftMost.binarySearchLeftMost(a, 90));
    }
}
